package com.store.pet.petstore.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.store.pet.petstore.R;
import com.store.pet.petstore.bean.MessageBean;
import com.store.pet.petstore.util.Constant;
import com.store.pet.petstore.util.GsonUtils;
import com.store.pet.petstore.util.OKHttpUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 注册界面
 */
public class RegisteredActivity extends BaseActivity {

    @BindView(R.id.my_status)
    View myStatus;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.password1_et)
    EditText password1Et;
    @BindView(R.id.password2_et)
    EditText password2Et;
    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.registered_1)
    Button registered1;
    @BindView(R.id.registered_2)
    Button registered2;
    @BindView(R.id.nickname_et)
    EditText nicknameEt;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;

    private String userSex = "";  //用户姓名

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_registered;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        //沉浸式标题栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        takeLinearStatusBar(myStatus);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.drawable.back);  //设置左上角图标
        toolbar.setNavigationOnClickListener(view -> finish());  //退出

        radioGroup.setOnCheckedChangeListener((radioGroup, i) -> {
            RadioButton rb = findViewById(i);
            userSex = rb.getText().toString();
        });
    }

    @Override
    public void loadData() {

    }

    private void userRegistered(int role) {
        String nickname = nicknameEt.getText().toString();  //昵称
        String psd1 = password1Et.getText().toString();  //第一次密码
        String psd2 = password2Et.getText().toString();  //再次输入密码
        String phone = phoneEt.getText().toString();  //手机
        if (!nickname.equals("") && !psd1.equals("") && !psd2.equals("")
                && !phone.equals("") && !userSex.equals("")) {
            if (psd1.equals(psd2)) {
                Map<String, Object> map = new HashMap<>();
                map.put("nickname", nickname);
                map.put("usersex", userSex);
                map.put("userpsd", psd1);
                map.put("userphone", phone);
                map.put("role", role);
                OKHttpUtils.requestData(Constant.REGISTERED, map, new OKHttpUtils.ServerCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        MessageBean messageBean = GsonUtils.parseMessage(data);
                        if (messageBean.getCode() == 101) {
                            finish();
                            hideKeyboard(getCurrentFocus());  //隐藏键盘
                        }
                        ToastUtils.showShort(messageBean.getMsg());
                    }

                    @Override
                    public void onFailed(String message) {
                        ToastUtils.showShort("注册失败");
                    }
                });
            } else {
                ToastUtils.showShort("两次密码不一致");
            }
        } else {
            ToastUtils.showShort("请输入完整");
        }
    }

    @OnClick({R.id.registered_1, R.id.registered_2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.registered_1:
                userRegistered(1);
                break;
            case R.id.registered_2:
                userRegistered(2);
                break;
        }
    }
}
